// ignore_for_file: non_constant_identifier_names

import 'package:flutter/material.dart';
import 'package:flutter_component_tool/src/widgets/cnm_toast/cnm_toast_status.dart';
import 'package:flutter_component_tool/src/widgets/cnm_toast/cnm_toast_theme.dart';
import 'package:flutter_component_tool/src/widgets/cnm_toast/cnm_toast_widget.dart';

/* 
 * 使用Toast前必须进行初始化 CNMToast.cnm_init() 操作
 */

class CNMToast {
  static BuildContext? _context;
  static final CNMToastTheme _globalTheme = CNMToastTheme();

  ///
  /// 初始化
  ///
  /// * [context] 上下文
  ///
  static void cnm_init(
    BuildContext context, {
    void Function(CNMToastTheme theme)? settingTheme,
  }) {
    _context = context;
    settingTheme?.call(_globalTheme);
  }

  ///
  /// 显示文本 Toast
  ///
  /// * [msg] 文本内容
  ///
  /// * [duration] 显示时长
  ///
  static void cnm_showText(
    String msg, {
    Duration duration = const Duration(milliseconds: 2500),
  }) {
    _cnm_showDialog(CNMToastStatus.text, message: msg);
    // duration 后消失
    _cnm_delayDismiss(duration);
  }

  ///
  /// 显示成功 Toast
  ///
  /// * [msg] 文本内容
  ///
  /// * [duration] 显示时长
  ///
  static void cnm_showSuccess({
    String? msg,
    Duration duration = const Duration(milliseconds: 2500),
  }) {
    _cnm_showDialog(CNMToastStatus.success, message: msg);
    // duration 后消失
    _cnm_delayDismiss(duration);
  }

  ///
  /// 显示失败 Toast
  ///
  /// * [msg] 文本内容
  ///
  /// * [duration] 显示时长
  ///
  static void cnm_showFail({
    String? msg,
    Duration duration = const Duration(milliseconds: 2500),
  }) {
    _cnm_showDialog(CNMToastStatus.fail, message: msg);
    // duration 后消失
    _cnm_delayDismiss(duration);
  }

  ///
  /// 显示网络请求加载 Circular Toast
  ///
  /// * [defaultIndicatorColor] 指示器颜色
  ///
  /// * [defaultIndicatorBackgroundColor] 指示器背景颜色
  ///
  static void cnm_showLoading({
    Color? defaultIndicatorColor,
    Color? defaultIndicatorBackgroundColor,
    Widget? child,
  }) {
    assert(_cnm_debugIsActive(_context));

    showDialog(
      context: _context!,
      barrierDismissible: _globalTheme.hideOnTouchMask,
      barrierColor: _globalTheme.maskBgColor,
      builder: (context) =>
          child ??
          Center(
            child: CircularProgressIndicator(
              color: defaultIndicatorColor,
              backgroundColor: defaultIndicatorBackgroundColor,
            ),
          ),
    );
  }

  ///
  /// 移除Toast
  ///
  static void cnm_dismiss() {
    if (_context!.mounted) {
      Navigator.pop(_context!);
    }
  }

  ///
  /// 统一 showDialog
  ///
  static void _cnm_showDialog(
    CNMToastStatus status, {
    String? message,
  }) {
    assert(_cnm_debugIsActive(_context));

    showDialog(
      context: _context!,
      barrierDismissible: _globalTheme.hideOnTouchMask,
      barrierColor: _globalTheme.maskBgColor,
      useSafeArea: false,
      builder: (context) => CnmToastWidget(
        status: status,
        theme: _globalTheme,
        msg: message,
      ),
    );
  }

  ///
  /// 延迟duration后消失
  ///
  static void _cnm_delayDismiss(Duration duration) {
    Future.delayed(duration, () => cnm_dismiss());
  }

  ///
  /// 判断context是否有效
  ///
  /// * [context] 上下文
  ///
  static bool _cnm_debugIsActive(BuildContext? context) {
    if (context == null) {
      throw FlutterError('context cannot be null, Set the value of context; and must carry out CNMToast.cnm_init().');
    } else {
      if (context is Element && !context.debugIsActive) {
        throw FlutterError.fromParts(<DiagnosticsNode>[
          ErrorSummary('This BuildContext is no longer valid.'),
          ErrorDescription('The showDialog function context parameter is a BuildContext that is no longer valid.'),
          ErrorHint(
            'This can commonly occur when the showDialog function is called after awaiting a Future. '
            'In this situation the BuildContext might refer to a widget that has already been disposed during the await. '
            'Consider using a parent context instead.',
          ),
        ]);
      }
    }

    return true;
  }
}
